package com.cyh.testcustomui;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Rect;
import android.graphics.RectF;
import android.util.AttributeSet;
import android.util.Log;
import android.view.View;

import androidx.annotation.Nullable;

public class QQStepView extends View {
    private static final String TAG = "QQStepView";
    private Context m_Context;

    private int m_outerColor = Color.RED;
    private int m_innerColor = Color.BLUE;
    private int m_borderWidth = 10;
    private int m_stepTextSize = 15;
    private int m_stepTextColor = Color.RED;

    private Paint m_OutPaint;
    private Paint m_InnerPaint;
    private Paint m_TextPaint;

    private int mStepMax = 0;
    private int mCurrentStep = 0;



    public QQStepView(Context context) {
        this(context, null);
    }

    public QQStepView(Context context, @Nullable AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public QQStepView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        m_Context = context;

        TypedArray array = context.obtainStyledAttributes(attrs, R.styleable.QQStepView);
        m_outerColor = array.getColor(R.styleable.QQStepView_outerColor, m_outerColor);
        m_innerColor = array.getColor(R.styleable.QQStepView_innerColor, m_innerColor);
        m_borderWidth = array.getDimensionPixelSize(R.styleable.QQStepView_borderWidth, m_borderWidth);
        m_stepTextSize = array.getDimensionPixelSize(R.styleable.QQStepView_stepTextSize, m_stepTextSize);
        m_stepTextColor = array.getColor(R.styleable.QQStepView_stepTextColor, m_stepTextColor);
        array.recycle();

        m_OutPaint = new Paint();
        m_OutPaint.setAntiAlias(true);
        m_OutPaint.setStrokeWidth(m_borderWidth);
        m_OutPaint.setColor(m_outerColor);
        m_OutPaint.setStyle(Paint.Style.STROKE);
        m_OutPaint.setStrokeCap(Paint.Cap.ROUND); //线头和线尾为圆弧型

        m_InnerPaint = new Paint();
        m_InnerPaint.setAntiAlias(true);
        m_InnerPaint.setStrokeWidth(m_borderWidth);
        m_InnerPaint.setColor(m_innerColor);
        m_InnerPaint.setStyle(Paint.Style.STROKE);
        m_InnerPaint.setStrokeCap(Paint.Cap.ROUND); //线头和线尾为圆弧型

        m_TextPaint = new Paint();
        m_TextPaint.setAntiAlias(true);
        m_TextPaint.setColor(m_stepTextColor);
        m_TextPaint.setTextSize(m_stepTextSize);


    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);

        //获取宽高模式
        int widthMode = MeasureSpec.getMode(widthMeasureSpec);
        int heightMode = MeasureSpec.getMode(heightMeasureSpec);
        Log.i(TAG, "-------获取宽高模式：widthMode = " + widthMode + ", heightMode = " + heightMode);

        int width = 0;
        int height = 0;
        if(widthMode == MeasureSpec.AT_MOST || heightMode == MeasureSpec.AT_MOST) {
            width = height = MyUtil.dp2px(m_Context, 40);
        } else {
            width = MeasureSpec.getSize(widthMeasureSpec);
            height = MeasureSpec.getSize(heightMeasureSpec);
        }

        setMeasuredDimension(width > height ? height : width, width > height ? height : width);
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);

        //画外圆
        int center = getWidth()/2;
        int redius = getWidth()/2 - m_borderWidth;
        RectF rectF = new RectF(center - redius, center - redius, center + redius, center + redius);
        canvas.drawArc(rectF, 135, 270, false, m_OutPaint);

        if(mStepMax == 0) {
            return;
        }

        //画内圆
        float sweepAngle = (float)mCurrentStep / mStepMax;
        canvas.drawArc(rectF, 135, sweepAngle * 270, false, m_InnerPaint);


        //画文字
        String stepText = mCurrentStep + "";
        Rect textBounds = new Rect();
        m_TextPaint.getTextBounds(stepText, 0, stepText.length(), textBounds);
        int dx = getWidth()/2 - textBounds.width()/2;
        //计算基线
        Paint.FontMetricsInt fontMetrics = m_TextPaint.getFontMetricsInt();
        int dy = (fontMetrics.bottom - fontMetrics.top)/2 - fontMetrics.bottom;
        int baseLine = getHeight()/2 + dy; //中线 + dy
        canvas.drawText(stepText, dx, baseLine, m_TextPaint);
    }

    public int getmStepMax() {
        return mStepMax;
    }

    public synchronized void setmStepMax(int mStepMax) {
        this.mStepMax = mStepMax;
    }

    public  int getmCurrentStep() {
        return mCurrentStep;
    }

    public synchronized void setmCurrentStep(int mCurrentStep) {
        this.mCurrentStep = mCurrentStep;
        invalidate();
    }
}
